package mylittleinvestigations.DialogCutscenes;

import java.io.IOException;
import mylittleinvestigations.CaseInformation.Case;
import mylittleinvestigations.CaseInformation.CaseXmlReader;
import mylittleinvestigations.CaseInformation.CaseXmlWriter;
import mylittleinvestigations.EasingFunctions.EasingFunction;
import mylittleinvestigations.EasingFunctions.LinearEase;
import mylittleinvestigations.Sprite;
import mylittleinvestigations.Vector2;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;

public class DialogCutsceneAnimationElement
{
    private String textureId;
    private Sprite sprite;

    private int originalXPosition;
    private int originalYPosition;

    private int originalATint;
    private int originalRTint;
    private int originalGTint;
    private int originalBTint;

    private EasingFunction xPositionEase;
    private EasingFunction yPositionEase;

    private EasingFunction aTintEase;
    private EasingFunction rTintEase;
    private EasingFunction gTintEase;
    private EasingFunction bTintEase;

    private int xPosition;
    private int yPosition;

    private int aTint;
    private int rTint;
    private int gTint;
    private int bTint;

    public Sprite getSprite()
    {
        if (this.sprite == null)
        {
            this.sprite = Case.getInstance().getSpriteManager().GetSpriteFromId(this.textureId);
        }

        return this.sprite;
    }

    public DialogCutsceneAnimationElement(String textureId, int x, int y, int a, int r, int g, int b)
    {
        this.textureId = textureId;
        this.sprite = null;

        this.originalXPosition = x;
        this.originalYPosition = y;

        this.originalATint = a;
        this.originalRTint = r;
        this.originalGTint = g;
        this.originalBTint = b;

        this.Reset();
    }

    public void Update(GameContainer gc, int delta)
    {
        if (this.xPositionEase != null)
        {
            this.xPositionEase.Update(gc, delta);
            this.xPosition = (int)this.xPositionEase.GetCurrentValue();
        }

        if (this.yPositionEase != null)
        {
            this.yPositionEase.Update(gc, delta);
            this.yPosition = (int)this.yPositionEase.GetCurrentValue();
        }

        if (this.aTintEase != null)
        {
            this.aTintEase.Update(gc, delta);
            this.aTint = (int)this.aTintEase.GetCurrentValue();
        }

        if (this.rTintEase != null)
        {
            this.rTintEase.Update(gc, delta);
            this.rTint = (int)this.rTintEase.GetCurrentValue();
        }

        if (this.gTintEase != null)
        {
            this.gTintEase.Update(gc, delta);
            this.gTint = (int)this.gTintEase.GetCurrentValue();
        }

        if (this.bTintEase != null)
        {
            this.bTintEase.Update(gc, delta);
            this.bTint = (int)this.bTintEase.GetCurrentValue();
        }
    }

    public void Draw(GameContainer gc, Graphics g)
    {
        this.getSprite().Draw(gc, g, new Vector2(this.xPosition, this.yPosition), new Color(this.rTint, this.gTint, this.bTint, this.aTint));
    }

    public void Finish()
    {
        if (this.xPositionEase != null)
        {
            this.xPositionEase.Finish();
            this.xPosition = (int)this.xPositionEase.GetCurrentValue();
        }

        if (this.yPositionEase != null)
        {
            this.yPositionEase.Finish();
            this.yPosition = (int)this.yPositionEase.GetCurrentValue();
        }

        if (this.aTintEase != null)
        {
            this.aTintEase.Finish();
            this.aTint = (int)this.aTintEase.GetCurrentValue();
        }

        if (this.rTintEase != null)
        {
            this.rTintEase.Finish();
            this.rTint = (int)this.rTintEase.GetCurrentValue();
        }

        if (this.gTintEase != null)
        {
            this.gTintEase.Finish();
            this.gTint = (int)this.gTintEase.GetCurrentValue();
        }

        if (this.bTintEase != null)
        {
            this.bTintEase.Finish();
            this.bTint = (int)this.bTintEase.GetCurrentValue();
        }
    }

    public void SetTargetPosition(int x, int y, int msDuration)
    {
        this.xPositionEase = new LinearEase(this.xPosition, x, msDuration);
        this.xPositionEase.Begin();
        this.yPositionEase = new LinearEase(this.yPosition, y, msDuration);
        this.yPositionEase.Begin();
    }

    public void SetTargetTint(int a, int r, int g, int b, int msDuration)
    {
        this.aTintEase = new LinearEase(this.aTint, a, msDuration);
        this.aTintEase.Begin();
        this.rTintEase = new LinearEase(this.rTint, r, msDuration);
        this.rTintEase.Begin();
        this.gTintEase = new LinearEase(this.gTint, g, msDuration);
        this.gTintEase.Begin();
        this.bTintEase = new LinearEase(this.bTint, b, msDuration);
        this.bTintEase.Begin();
    }

    public void Reset()
    {
        this.xPosition = this.originalXPosition;
        this.yPosition = this.originalYPosition;

        this.aTint = this.originalATint;
        this.rTint = this.originalRTint;
        this.gTint = this.originalGTint;
        this.bTint = this.originalBTint;

        this.ClearEasingFunctions();
    }

    public void ClearEasingFunctions()
    {
        this.xPositionEase = null;
        this.yPositionEase = null;

        this.aTintEase = null;
        this.rTintEase = null;
        this.gTintEase = null;
        this.bTintEase = null;
    }

    public void SaveToXml(CaseXmlWriter writer) throws IOException
    {
        writer.StartElement("DialogCutsceneAnimationElement");
        writer.WriteSpriteId("SpriteId", this.textureId);
        writer.WriteIntElement("OriginalXPosition", this.originalXPosition);
        writer.WriteIntElement("OriginalYPosition", this.originalYPosition);
        writer.WriteIntElement("OriginalATint", this.originalATint);
        writer.WriteIntElement("OriginalRTint", this.originalRTint);
        writer.WriteIntElement("OriginalGTint", this.originalGTint);
        writer.WriteIntElement("OriginalBTint", this.originalBTint);
        writer.EndElement();
    }

    public DialogCutsceneAnimationElement(CaseXmlReader reader)
    {
        reader.StartElement("DialogCutsceneAnimationElement");
        this.textureId = reader.ReadTextElement("SpriteId");
        this.originalXPosition = reader.ReadIntElement("OriginalXPosition");
        this.originalYPosition = reader.ReadIntElement("OriginalYPosition");
        this.originalATint = reader.ReadIntElement("OriginalATint");
        this.originalRTint = reader.ReadIntElement("OriginalRTint");
        this.originalGTint = reader.ReadIntElement("OriginalGTint");
        this.originalBTint = reader.ReadIntElement("OriginalBTint");
        reader.EndElement();
    }

    void markChildSprites(String markParent)
    {
        getSprite().markChildSprites(markParent);
    }
}